<?php
require_once 'RenrenOAuthApiService.class.php';
require_once 'RenrenRestApiService.class.php';
define('RR_SCOPE',"publish_feed,photo_upload");

function getRrLoginUrl($appkey)
{
	global $_FANWE;
	$state = md5(uniqid(rand(),TRUE));
    $url = "https://graph.renren.com/oauth/authorize?&response_type=code&client_id=" 
        . $appkey . "&redirect_uri=".urlencode($_FANWE['site_url']."callback/renren.php")
        . "&state=" .$state
        . "&scope=".RR_SCOPE;
	
	fSetCookie('rr_state',$state);
    return $url;
}

function getRrAccessToken($appkey,$appsecet)
{
	global $_FANWE;
	$rr_state = $_FANWE['cookie']['rr_state'];
	if($_REQUEST['state'] == $rr_state)
    {
    	$code = $_GET["code"];
    	if($code){
    	$oauthApi = new RenrenOAuthApiService;
		$post_params = array('client_id'=>$appkey,
				'client_secret'=>$appsecet,
				'redirect_uri'=>$_FANWE['site_url']."callback/renren.php",
				'grant_type'=>'authorization_code',
				'code'=>$code
				);
		$token_url='http://graph.renren.com/oauth/token';
		$access_info=$oauthApi->rr_post_curl($token_url,$post_params);//使用code换取token
		//$access_info=$oauthApi->rr_post_fopen($token_url,$post_params);//如果你的环境无法支持curl函数，可以用基于fopen函数的该函数发送请求
		$access_token=$access_info["access_token"];
		return $access_token;
    	}else{
    		echo 'errorparameter code is null:<br>'.$_SERVER["QUERY_STRING"];
    	}
    	   	
/*        $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
            . "client_id=" . $appid. "&redirect_uri=" . urlencode($_FANWE['site_url']."callback/qq.php")
            . "&client_secret=" . $appkey. "&code=" . $_REQUEST["code"];

        $response = file_get_contents($token_url);
        if (strpos($response, "callback") !== false)
        {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response  = substr($response, $lpos + 1, $rpos - $lpos -1);
            $msg = json_decode($response);
            if (isset($msg->error))
            {
                echo "<h3>error:</h3>" . $msg->error;
                echo "<h3>msg  :</h3>" . $msg->error_description;
                exit;
            }
        }

        $params = array();
        parse_str($response, $params);
        return $params["access_token"];*/

    }
    else 
    {
        echo("The state does not match. You may be a victim of CSRF.");
    }
}

/*function getQqOpenid($access_token)
{
    $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$access_token;
    $str  = file_get_contents($graph_url);
    if (strpos($str, "callback") !== false)
    {
        $lpos = strpos($str, "(");
        $rpos = strrpos($str, ")");
        $str  = substr($str, $lpos + 1, $rpos - $lpos -1);
    }

    $user = json_decode($str);
    if (isset($user->error))
    {
        echo "<h3>error:</h3>" . $user->error;
        echo "<h3>msg  :</h3>" . $user->error_description;
        exit;
    }
    return $user->openid;
}*/

function getRrUserInfo($access_token)
{
   $restApi = new RenrenRestApiService;
	$params = array('fields'=>'uid,name,sex,birthday,mainurl,hometown_location,university_history,tinyurl,headurl','access_token'=>$access_token);
	$res = $restApi->rr_post_curl('users.getInfo', $params);//curl函数发送请求
    return $res[0];
}
?>